package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ResUtil;

import java.lang.reflect.Field;

public class EditTextAndDel extends LinearLayout {
    private EditText mEditText;
    private ImageView mImageView;

    private int mTextColor;
    private int mTextSize;
    private int mDelResId;
    private String mText;

    public EditTextAndDel(Context context) {
        this(context, null);
    }

    public EditTextAndDel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditTextAndDel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        init(context);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) return;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EditTextAndDel);
        mTextColor = ta.getColor(R.styleable.EditTextAndDel_et_textColor, ResUtil.getColor(R.color.color_text));
        mDelResId = ta.getResourceId(R.styleable.EditTextAndDel_iv_icon, R.mipmap.delete);
        mTextSize = (int) ta.getDimension(R.styleable.EditTextAndDel_et_textSize, 16);
        ta.getText(R.styleable.EditTextAndDel_et_text);
        ta.recycle();

    }

    public void init(Context context) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        LayoutParams lp = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        mEditText = new EditText(context);
        mEditText.setTextSize(mTextSize);
        mEditText.setTextColor(mTextColor);
        mEditText.setText(mText);
        mEditText.setBackground(null);
        mEditText.setLayoutParams(lp);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    if (mImageView.getVisibility() != View.VISIBLE)
                        mImageView.setVisibility(VISIBLE);
                } else mImageView.setVisibility(INVISIBLE);
            }
        });
        addView(mEditText);

        LayoutParams IvLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mImageView = new ImageView(context);
        mImageView.setImageResource(mDelResId);
        mImageView.setVisibility(INVISIBLE);
        mImageView.setLayoutParams(IvLp);
        mImageView.setOnClickListener(v -> {
            if (mEditText != null) mEditText.setText("");
        });
        addView(mImageView);
    }

    public void setCursorColor(int color){
        if (mEditText!=null) {
            try {
                Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
                field.setAccessible(true);
                field.set(mEditText,color);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setTypeface(Typeface typeface){
        if (mEditText!=null)mEditText.setTypeface(typeface);
    }

    public void setInputType(int type){
        if (mEditText!=null)mEditText.setInputType(type);
    }

    public void setMaxLength(int length){
        if (mEditText!=null)mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    public void setIvWidth(int width,int height){
        if (mImageView!=null){
            LayoutParams lp = (LayoutParams) mImageView.getLayoutParams();
            lp.width = width;
            lp.height = height;
            mImageView.setLayoutParams(lp);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public void setEditFilters(InputFilter[] inputFilters){
        if (mEditText!=null)
        {
            mEditText.setFilters(inputFilters);
        }
    }

    public EditText getEditText() {
        return mEditText;
    }

}
